package cn.xzhang.boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.xzhang.boot.common.exception.ServiceException;
import cn.xzhang.boot.common.pojo.PageResult;
import cn.xzhang.boot.mapper.CarouselMapper;
import cn.xzhang.boot.model.dto.carousel.CarouselAddReqDTO;
import cn.xzhang.boot.model.dto.carousel.CarouselPageReqDTO;
import cn.xzhang.boot.model.dto.carousel.CarouselUpdateReqDTO;
import cn.xzhang.boot.model.entity.Carousel;
import cn.xzhang.boot.model.vo.carousel.CarouselSimpleVo;
import cn.xzhang.boot.model.vo.carousel.CarouselVo;
import cn.xzhang.boot.service.CarouselService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static cn.xzhang.boot.common.exception.enums.GlobalErrorCodeConstants.*;
import static cn.xzhang.boot.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 针对表【carousel(轮播图表)】的数据库操作Service实现
 *
 * @author <a href="https://github.com/XiaoZhangCode">XiaoZhangCode</a>
 */
@Service
public class CarouselServiceImpl extends ServiceImpl<CarouselMapper, Carousel> implements CarouselService {

    @Resource
    private CarouselMapper carouselMapper;

    /**
     * 添加新轮播图
     *
     * @param carouselReqDTO 轮播图信息请求DTO
     * @return 添加成功返回轮播图id
     */
    @Override
    public long addCarousel(CarouselAddReqDTO carouselReqDTO) {
        Carousel carousel = new Carousel();
        BeanUtil.copyProperties(carouselReqDTO, carousel);
        if (!this.save(carousel)) {
            throw exception(ADD_FAIL);
        }
        return carousel.getId();
    }

    /**
     * 更新轮播图信息
     *
     * @param carouselReqDTO 轮播图信息更新请求DTO
     * @return 更新成功返回true
     */
    @Override
    public boolean updateCarousel(CarouselUpdateReqDTO carouselReqDTO) {
        if (carouselReqDTO.getId() == null) {
            throw exception(BAD_REQUEST);
        }
        Carousel carousel = new Carousel();
        BeanUtil.copyProperties(carouselReqDTO, carousel);
        boolean b = this.updateById(carousel);
        if (!b) {
            throw exception(UPDATE_FAIL);
        }
        return true;
    }

    /**
     * 删除轮播图
     *
     * @param id 轮播图id
     * @return 删除成功返回true
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public boolean deleteCarousel(Long id) {
        if (id == null) {
            throw exception(BAD_REQUEST);
        }
        boolean b = this.removeById(id);
        if (!b) {
            throw exception(DELETE_FAIL);
        }
        return true;
    }

    /**
     * 将轮播图对象转换为轮播图VO对象
     *
     * @param carousel 轮播图对象
     * @return 返回轮播图VO对象
     */
    @Override
    public CarouselSimpleVo getSimpleCarouselVO(Carousel carousel) {
        if (carousel == null) {
            return null;
        }
        CarouselSimpleVo carouselSimpleVo = new CarouselSimpleVo();
        BeanUtil.copyProperties(carousel, carouselSimpleVo);
        return carouselSimpleVo;
    }

    /**
     * 获取轮播图分页信息
     *
     * @param carouselPageReqDTO 轮播图分页请求DTO
     * @return 返回轮播图分页结果
     */
    @Override
    public PageResult<CarouselVo> getCarouselPage(CarouselPageReqDTO carouselPageReqDTO) {
        PageResult<Carousel> pageResult = carouselMapper.selectPage(carouselPageReqDTO);
        if (pageResult.getList() == null) {
            return PageResult.empty();
        }
        List<CarouselVo> carouselVos = pageResult.getList().stream().map(carousel -> {
            CarouselVo carouselVo = new CarouselVo();
            BeanUtil.copyProperties(carousel, carouselVo);
            return carouselVo;
        }).collect(Collectors.toList());
        return new PageResult<>(carouselVos, pageResult.getTotal());
    }

    @Override
    public CarouselVo getCarouselVO(Carousel carousel) {
        if (carousel == null) {
            return null;
        }
        CarouselVo carouselVo = new CarouselVo();
        BeanUtil.copyProperties(carousel, carouselVo);
        return carouselVo;
    }

}



